<template>
	<view class="index">
		<!-- 导航栏 -->
		<view class="bg">
			<view class="search" @tap="toSearch()">
				<uni-icon type="search"></uni-icon>
				请输入你需要的商品名称
			</view>
		</view>

		<goods-list :goods="goodsList" :price-name="priceName" goods-type="jd"></goods-list>

	</view>
</template>

<script>
	import uniIcon from '../../components/uni-icon.vue';
	import goodsList from '../../components/coupon-goods-list.vue';
	import UniNavBar from '../../components/uni-nav-bar.vue';
	import banner from '../../components/banner.vue';
	import base from '../../common/base.js';
	import {
		priceName
	} from '../../common/config.js';
	import Request from '../../common/request.js';

	export default {
		components: {
			uniIcon,
			goodsList,
			banner,
			UniNavBar
		},
		data() {
			return {
				keyword: '',
				page: 1,
				bannerList: [],
				goodsList: [],
				nextPage: true,
			}
		},
		onLoad() {
			this.getIndexData();
			this.priceName = priceName;
		},

		onReachBottom() {
			if (this.keyword) {
				this.searchGoods();
			} else {
				this.getIndexData();
			}
		},
		methods: {
			//获取首页数据
			getIndexData: function() {
				if (this.nextPage == false) {
					return false;
				}

				var that = this;
				if(that.page == 1){
					uni.showLoading();
				}
				
				var params = {
					url: 'jd/getRecommendGoods',
					data: {
						page: that.page
					},
					sCallback: function(res) {
						uni.hideLoading();
						that.goodsList = that.goodsList.concat(res.data);
						that.page++;
						if (res.length < 20) {
							that.nextPage = false
						}
					}
				}
				Request.send(params);
			},
			//搜索商品
			searchGoods: function() {
				if (this.nextPage == false) {
					return false;
				}
				var keyword = this.keyword;
				var that = this;
				if(that.page == 1){
					uni.showLoading();
				}
				var params = {
					url: 'jd/search',
					data: {
						page: that.page,
						goods_url: keyword
					},
					sCallback: function(res) {

						that.goodsList.push(res);
						that.page++;
						if (res.length < 20) {
							that.nextPage = false
						}
					}
				}
				Request.send(params);
			},

			//搜索
			search: function() {
				var that = this;
				this.goodsList = [];
				this.page = 1;
				this.nextPage = true;
				this.searchGoods();
			},
			toSearch: function() {
				uni.navigateTo({
					url: '/pages/coupon/search?type_index=1'
				})
			},
		}
	}
</script>

<style>
	view {
		display: flex;
		font-size: 26upx;
	}


	.index {
		width: 100%;
		flex-direction: column;
		background: #F5F5F5;

	}

	.bg {
		background: linear-gradient(to right, #FAEAD0 0, #E3BD8E 100%);
		padding: 60upx 0;
		justify-content: center;
	}

	.bg .search {
		background: #fff;
		height: 60upx;
		align-items: center;
		padding: 0 40upx;
		border-radius: 30upx;
		width: 80%;
		color: #929393;
	}
</style>
